<?php

class DatabaseSeeder extends Seeder {

	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run()
	{
		Eloquent::unguard();

		$this->call('DataSeeder');
		$this->call('UserSeeder');

	}

}

/**
* 
*/
class DataSeeder extends Seeder
{
	
	public function run()
	{
		DB::table("companys")->insert(array("厂商名"=>"本田","地址"=>"广州"));
		DB::table("companys")->insert(array("厂商名"=>"奔驰","地址"=>"广州"));
		DB::table("companys")->insert(array("厂商名"=>"宝马","地址"=>"广州"));
		DB::table("companys")->insert(array("厂商名"=>"丰田","地址"=>"广州"));

		DB::table("cars")->insert(array("型号"=>"雅阁","公司"=>"1","进货价格"=>"11","出售价格"=>"17","年份"=>"2015"));
		DB::table("cars")->insert(array("型号"=>"飞度","公司"=>"1","进货价格"=>"5","出售价格"=>"7","年份"=>"2015"));	

		DB::table("cars")->insert(array("型号"=>"奔驰M级","公司"=>"2","进货价格"=>"70","出售价格"=>"80","年份"=>"2015"));	
		DB::table("cars")->insert(array("型号"=>"奔驰B级","公司"=>"2","进货价格"=>"15","出售价格"=>"20","年份"=>"2015"));	

		DB::table("cars")->insert(array("型号"=>"宝马X3","公司"=>"3","进货价格"=>"40","出售价格"=>"45","年份"=>"2015"));	
		DB::table("cars")->insert(array("型号"=>"宝马X5","公司"=>"3","进货价格"=>"80","出售价格"=>"95","年份"=>"2015"));	

		DB::table("clients")->insert(array("姓名"=>"张三","性别"=>"1","地址"=>"广州","电话"=>"13760558883","年龄"=>"44"));	
		DB::table("clients")->insert(array("姓名"=>"李四","性别"=>"2","地址"=>"广州","电话"=>"13760558883","年龄"=>"25"));	

/*		DB::table("buy")->insert(array("公司"=>"1","订货日期"=>"2015-04-14","到货日期"=>"2015-04-14"));	
		DB::table("buydetail")->insert(array("订单号"=>"1","型号"=>"1","数量"=>"20","价格"=>"20"));	

		DB::table("buy")->insert(array("公司"=>"1","订货日期"=>"2015-04-14","到货日期"=>"2015-04-14"));	
		DB::table("buydetail")->insert(array("订单号"=>"2","型号"=>"2","数量"=>"20","价格"=>"7"));	

		DB::table("buy")->insert(array("公司"=>"4","订货日期"=>"2015-04-14","到货日期"=>"2015-04-14"));	
		DB::table("buydetail")->insert(array("订单号"=>"3","型号"=>"5","数量"=>"20","价格"=>"40"));		
*/
		for ($i=0; $i < 20; $i++) { 
			$order = new Buy;
			$order->公司 = rand(1,3);
			$order->订货日期 = "2015-04-01";
			$order->到货日期 = "2015-04-01";
			$order->created_at = $this->randomDate("2015-01-01 00:00:00","2015-04-30 00:00:00");
			$order->save();

			$orderdetail = new BuyDetail;
			$orderdetail->订单号 = $order->id;
			$orderdetail->型号 = $order->公司*rand(1,2);
			$orderdetail->数量 = rand(1,20);
			$orderdetail->价格 = rand(10,20);
			$orderdetail->created_at = $this->randomDate("2015-01-01 00:00:00","2015-04-30 00:00:00");
			$orderdetail->save();

			$isstore = Store::where("车型",$orderdetail->型号)->first();

			if($isstore){
				$isstore->数量 = $isstore->数量+$orderdetail->数量;
				$isstore->save();
			}else{
				$store = new Store;
				$store->车型 = $orderdetail->型号;
				$store->数量 = $orderdetail->数量;
				$store->save();
			}

		}

		for ($j=0; $j < 20; $j++) { 
			$store = Store::where("车型",rand(1,6))->first();
			$order = new Sell;
			$order->客户 = rand(1,2);
			$order->订货日期 = 0;
			$order->交货日期 = 0;
			$order->save();

			$car = rand(1,6);
			$isstore = Store::where("车型",$car)->first();
			if($isstore){
				if($isstore->数量>0){
					$number = rand(1,$isstore->数量);
					$orderdetail =new SellDetail;
					$orderdetail->订单号 = $order->id;
					$orderdetail->车型 = $car;
					$orderdetail->数量 = $number;
					$orderdetail->售价 = rand(10,100);
					$orderdetail->created_at = $this->randomDate("2015-01-01 00:00:00","2015-04-30 00:00:00");
					$orderdetail->save();

					$isstore->数量 = $isstore->数量 - $number;
					$isstore->save();
				}
			}
		}
	}

	public function randomDate($begintime, $endtime = "") {
		$begin = strtotime($begintime);
		$end = $endtime == "" ? mktime() : strtotime($endtime);
		$timestamp = rand($begin, $end);
		return date("Y-m-d H:i:s", $timestamp);
	}
}

/**
* 
*/
class UserSeeder extends Seeder
{
	
	public function run()
	{
		DB::table("users")->insert(array("username"=>"a","password"=>Hash::make("a"),
					"销售管理"=>1,"仓库管理"=>1,"财务管理"=>1,"经营分析"=>1,"基础数据"=>1,
					"系统维护"=>1));
	}
}